// -------------------------------------------------------------------------
//    @FileName         :    NFCLogModule.h
//    @Author           :    LvSheng.Huang
//    @Date             :    2012-12-15
//    @Module           :    NFCLogModule
//    @Desc             :
// -------------------------------------------------------------------------

#ifndef NFC_LOG_MODULE_H
#define NFC_LOG_MODULE_H

#define USE_ASYN_LOG 1

#include "NFComm/NFPluginModule/NFILogModule.h"

#ifdef USE_ASYN_LOG
#include "NFComm/NFPluginModule/NFIActorModule.h"

class NFCLogComponent : public NFIComponent
{
public:
    NFCLogComponent() : NFIComponent(GET_CLASS_NAME(NFCLogComponent))
    {
    }

    virtual ~NFCLogComponent()
    {

    }

    virtual bool    InitLog();
    virtual bool    Init();
    virtual bool    AfterInit();
    virtual bool    Shut();
    virtual int     OnASyncEvent(const NFGUID& self, const int from, const int nSubMessageID, std::string& arg);
    virtual bool    Log(const NFILogModule::NF_LOG_LEVEL nll, const std::string& log);
public:
    static bool     CheckLogFileExist(const char* filename);
    static void     rolloutHandler(const char* filename, std::size_t size);
public:
    void            SetStrLogConfigName(const std::string& configName)
    {
        strLogConfigName = configName;
    }
    void            SetAppId(int id)
    {
        appId = id;
    }
    void            SetAppName(const std::string& name)
    {
        appName = name;
    }
    void            SetStrAppLogName(const std::string& configName)
    {
        strAppLogName = configName;
    }
private:
    static unsigned int         idx;
    uint64_t                     mnLogCountTotal;
    std::string                 strLogConfigName;
    int                         appId;
    std::string                 appName;
    std::string                 strAppLogName;
};
#endif

class NFCLogModule
    : public NFILogModule
{
public:

    NFCLogModule(NFIPluginManager* p);
    virtual ~NFCLogModule() {}

    virtual bool Awake();
    virtual bool Init();
    virtual bool Shut();
    virtual bool Finalize();

    virtual bool BeforeShut();
    virtual bool AfterInit();

    virtual bool Execute();

    ///////////////////////////////////////////////////////////////////////
    virtual void LogStack();

    virtual bool LogElement(const NF_LOG_LEVEL nll, const NFGUID ident, const std::string& strElement, const std::string& strDesc, const char* func = "", int line = 0);
    virtual bool LogProperty(const NF_LOG_LEVEL nll, const NFGUID ident, const std::string& strProperty, const std::string& strDesc, const char* func = "", int line = 0);
    virtual bool LogRecord(const NF_LOG_LEVEL nll, const NFGUID ident, const std::string& strRecord, const std::string& strDesc, const int nRow, const int nCol, const char* func = "", int line = 0);
    virtual bool LogRecord(const NF_LOG_LEVEL nll, const NFGUID ident, const std::string& strRecord, const std::string& strDesc, const char* func = "", int line = 0);
    virtual bool LogObject(const NF_LOG_LEVEL nll, const NFGUID ident, const std::string& strDesc, const char* func = "", int line = 0);

    virtual bool LogNormal(const NF_LOG_LEVEL nll, const NFGUID ident, const std::string& strInfo, const char* func = "", int line = 0);
    virtual bool LogNormal(const NF_LOG_LEVEL nll, const NFGUID ident, const std::string& strInfo, const int64_t nDesc, const char* func = "", int line = 0);
    virtual bool LogNormal(const NF_LOG_LEVEL nll, const NFGUID ident, const std::string& strInfo, const std::string& strDesc, const char* func = "", int line = 0);
    virtual bool LogNormal(const NF_LOG_LEVEL nll, const NFGUID ident, const std::ostringstream& stream, const char* func = "", int line = 0);

    virtual bool LogDebugFunctionDump(const NFGUID ident, const int nMsg, const std::string& strArg, const char* func = "", const int line = 0);
    virtual bool ChangeLogLevel(const std::string& strLevel);
protected:
#ifdef USE_ASYN_LOG
    virtual bool    StartActorPool(const int nCount);
    virtual bool    CloseActorPool();
    int             RequestAsyEnd(const NFGUID& self, const int nFormActor, const int nEventID, const std::string& strData);
    bool            AsynLog(const NF_LOG_LEVEL nil, const std::string& log);
    int             GetActor();
#endif
protected:
    virtual bool Log(const NF_LOG_LEVEL nll, const char* format, ...);

    static bool CheckLogFileExist(const char* filename);
    static void rolloutHandler(const char* filename, std::size_t size);

private:
    static unsigned int idx;
    uint64_t mnLogCountTotal;
    std::string strLogConfigName;
    std::string strAppLogName;
private:
#ifdef USE_ASYN_LOG
    NFIActorModule* m_pActorModule;
    NFMapEx<int, int> mActorList; //actorid <-->Used
    int mnSuitIndex;
#endif
};

#endif
